﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using QuanLyVanBanBUS;

public partial class QLVB_User_DangNhap : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnDangNhap_Click(object sender, EventArgs e)
    {
        MyTextBox myTextBox = new MyTextBox();
        string maNguoiDung = string.Empty;
        string matKhau = string.Empty;

        //Kiem tra rong
        if (myTextBox.kiemtraRong(txtMaNguoiDung) == false)
        {
            if (myTextBox.kiemtraRong(txtMatKhau) == false)
            {
                maNguoiDung = txtMaNguoiDung.Text;

                matKhau = myTextBox.mahoaSHA1(txtMatKhau);

                //Phan quyen
                NguoiDungBUS ndBUS = new NguoiDungBUS();

                bool kiemtra = ndBUS.kiemtraDangNhap(maNguoiDung, matKhau);
                if (kiemtra == true)
                {
                    //Lay MaNguoiDung
                    Session["MaNguoiDung"] = txtMaNguoiDung.Text;

                    //Lay MaPhong theo MaNguoidung
                    Session["MaPhong"] = (new PhongBUS()).layMaPhongTheoMaNguoiDung(txtMaNguoiDung.Text);

                    //
                    LoaiNguoiDungBUS lndBUS = new LoaiNguoiDungBUS();

                    Session["MaLoaiNguoiDung"] = lndBUS.layMaLoaiNguoiDungTheoMaNguoiDung(maNguoiDung);

                    switch (Session["MaLoaiNguoiDung"].ToString())
                    {
                        case "ADMIN":
                            Response.Redirect("~/QLVB/Admin/aHome.aspx");
                            break;

                        case "USER":
                            Response.Redirect("SoanThao.aspx");
                            break;

                        case "KSCN":
                            Response.Redirect("DuyetSoanThao.aspx");
                            break;

                        case "KSPT":
                            Response.Redirect("DuyetSoanThao.aspx");
                            break;
                    }
                }
            }
            else
            {
                Alert.Show("Mat khau khong the rong");

                return;
            }
        }
        else
        {
            Alert.Show("Ma nguoi dung khong the rong");

            return;
        }
    }
    protected void btnDong_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/pHome.aspx");
    }
}